package com.jisf.driver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jisf.driver.entity.po.DrivingSchoolLog;
import com.jisf.driver.entity.po.Student;
import com.jisf.driver.entity.po.User;
import com.jisf.driver.entity.vo.StudentMessageResponseVo;
import com.jisf.driver.entity.vo.StudentMessageVo;
import com.jisf.driver.entity.vo.UserListRequestVo;
import com.jisf.driver.exception.SystemException;
import com.jisf.driver.mapper.StudentMapper;
import com.jisf.driver.mapper.UserMapper;
import com.jisf.driver.response.ResultCode;
import com.jisf.driver.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jisf.driver.utils.LogUtil;
import com.jisf.driver.utils.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jisf
 * @since 2022-05-06
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogUtil log;

    /**
     * 学员获取个人信息
     * @return
     */
    @Override
    public StudentMessageVo getMessage() {
        try {
            Integer userid = UserUtil.userIdThreadLocal.get();
            StudentMessageVo message = studentMapper.getMessage(userid);
            return message;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 学员完善个人信息
     * @param studentMessageVo
     * @return
     */
    @Override
    public ResultCode completeMessage(StudentMessageVo studentMessageVo) {
        try {
            User user = new User();
            BeanUtils.copyProperties(studentMessageVo, user);
            int update = userMapper.updateById(user);

            // 存储日志
            String operator = UserUtil.usernameThreadLocal.get();
            String msg = String.format("用户名为%s的用户完善自己的信息", operator);

            if (update != 0){
                String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, true));
                log.info(drivingSchoolLog);
                return ResultCode.MESSAGE_COMPLETE_SUCCESS;
            }else {
                String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, false));
                log.info(drivingSchoolLog);
                return ResultCode.MESSAGE_COMPLETE_FAILED;
            }
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 查询所有学员
     * @return
     */
    @Override
    public StudentMessageResponseVo allStudent(UserListRequestVo userListRequestVo) {
        try {
            Page<User> page = new Page<>(userListRequestVo.getPage(), userListRequestVo.getSize());
            List<StudentMessageVo> message = studentMapper.allStudent(userListRequestVo, page);
            long total = page.getTotal();
            StudentMessageResponseVo studentMessageResponseVo = new StudentMessageResponseVo(message, total);
            return studentMessageResponseVo;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 修改学员信息
     * @param studentMessageVo
     * @return
     */
    @Transactional
    @Override
    public ResultCode updateStudent(StudentMessageVo studentMessageVo) {
        try {
            User user = new User();
            BeanUtils.copyProperties(studentMessageVo, user);
            int updateById = userMapper.updateById(user);
            Student student = new Student();
            BeanUtils.copyProperties(studentMessageVo, student);
            UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("userid", studentMessageVo.getId());
            int update = studentMapper.update(student, updateWrapper);

            // 存储日志
            String operator = UserUtil.usernameThreadLocal.get();
            String msg = String.format("用户名为%s的用户修改了用户名为%s的用户的信息", operator, studentMessageVo.getUsername());

            if (update != 0 && updateById != 0) {
                String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, true));
                log.info(drivingSchoolLog);
                return ResultCode.MESSAGE_UPDATE_STUDENT_SUCCESS;
            }
            String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, false));
            log.info(drivingSchoolLog);
            return ResultCode.MESSAGE_UPDATE_STUDENT_FAILED;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }
}
